﻿#include "http-proxy.h"

HttpProxy::HttpProxy() {
    this->initialize_winsock();
    this->init();
}

HttpProxy::~HttpProxy() {
    this->destroy();
}

int initialize_winsock() {
    WSADATA wsaData;
    int     iResult;
    // 初始化 Winsock 库
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        std::cout << "WSAStartup failed: " << iResult << std::endl;
        return -1;
    }
    return 0;
}

void HttpProxy::init() {
    nets::HttpsService https;
    https.run(nullptr);

    nets::NetServer server;
    server.load_template();
    int socket_ = server.create_socket(8080);
    server.get_listen(socket_);
    server.accept_connection(socket_);
}

YAML::Node HttpProxy::load_config() {
    filesystem::path p("conf\\app.yaml");
    cout << "File Exists: " << filesystem::exists(p) << endl;

    std::ifstream     file(p);
    std::string       line;
    std::stringstream conf_content;
    while (std::getline(file, line)) {
        conf_content << line << endl;
    }
    file.close();
    cout << conf_content.str() << endl;

    try {
        YAML::Node  _CONFIG = YAML::Load(conf_content.str());
        const auto &server  = _CONFIG["server"];

        return server;
    } catch (runtime_error e) {
        cout << "Parse Failed: " << e.what() << endl;
    }

    YAML::Node nul;
    return nul;
}